﻿namespace ThingsGateway.Foundation
{
    public static class ResultExtensions
    {

        #region Public Methods
        /// <summary>
        /// 复制信息，不包含泛型类
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="result"></param>
        /// <returns></returns>
        public static Result<T> Copy<T>(this Result result)
        {
            Result<T> failedResult = new Result<T>
            {
                ResultCode = result.ResultCode,
                Message = result.Message,
            };
            return failedResult;
        }
        /// <summary>
        /// 复制信息，不包含泛型类
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="result"></param>
        /// <returns></returns>
        public static Result<T1, T2> Copy<T1, T2>(this Result result)
        {
            Result<T1, T2> failedResult = new Result<T1, T2>
            {
                ResultCode = result.ResultCode,
                Message = result.Message
            };
            return failedResult;
        }
        public static Result Copy(this Result result)
        {
            Result failedResult = new Result
            {
                ResultCode = result.ResultCode,
                Message = result.Message
            };
            return failedResult;
        }

        public static Result Then(this Result result, Func<Result> func)
        {
            return !result.IsSuccess ? result : func();
        }

        public static Result<T> Then<T>(this Result result, Func<Result<T>> func)
        {
            return !result.IsSuccess ? result.Copy<T>() : func();
        }

        public static Result<T1, T2> Then<T1, T2>(this Result result, Func<Result<T1, T2>> func)
        {
            return !result.IsSuccess ? result.Copy<T1, T2>() : func();
        }

        #endregion Public Methods

    }
}
